// pages/WebView/index.js
let app = getApp();
const HttpAjax = getApp().getHttpData;
import Toast from '../../components/dist/toast/toast';
import {Base64Encrypt,Base64Decrypt,Decrypt,EncryptAES,SHA256,RSA_encrypt,RSA} from '../../utils/crypto_RSA_AES'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    url: '',
    ishaspep: false,//没账套
    isshow:false,
    loadendtxt: '当前没绑定账套,去添加账套',
    customerporma:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options,options.CustomerName, options.UserName, options.UserPassWord, options.authMode);
    
    // setTimeout(()=>{
    //   console.log(this.data.url);
    // },500)
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log(1223)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      url: '',
      ishaspep: false,//没账套
      isshow:false,
      loadendtxt: '当前没绑定账套,去添加账套',
    })
    this.GetCustomerList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  GetCustomerList() {
    let that = this;
    Toast.loading({
      mask: true,
      message: '数据加载中'
    });
    HttpAjax(app.globalData.url + '/Door/GetCustomerList', {}, 'post', app.globalData.headerCookie).then(res => {
      let rest = res.data;
      setTimeout(()=>{
        that.setData({
          isshow: true
        })
      },500)
      
      if (rest.result == 1) {
        Toast.clear()
        if (rest.customerlist.length >= 1) {
          let options = rest.customerlist[0];
          let CustomerName = options.CName;
          let UserPassWord = Base64Decrypt(options.PPassword,wx.getStorageSync('AKey'),wx.getStorageSync('AIV'));

          let UserName, authMode;
          let arr = ['MobilePhone', 'CardData', 'IdentityCard'];
          for (let i = 0; i < arr.length; i++) {
            if (options[arr[i]]) {
              UserName = options[arr[i]];
              authMode = arr.length - i;
              break;
            }
          }
          console.log(`https://yun.pc15.net/WebUI/phone/PersonWap/index.html#/Index?CustomerName=${CustomerName}&UserName=${UserName}&UserPassWord=${UserPassWord}&authMode=${authMode}`)
          that.setData({
            customerporma: rest.customerlist[0],
            url: `https://yun.pc15.net/WebUI/phone/PersonWap/index.html#/Index?CustomerName=${CustomerName}&UserName=${UserName}&UserPassWord=${UserPassWord}&authMode=${authMode}`,
            // url: `https://192.168.1.168:8080/#/Index?CustomerName=${CustomerName}&UserName=${UserName}&UserPassWord=${UserPassWord}&authMode=${authMode}`,
            ishaspep: true
          });
         
        } else {
          Toast.clear()
          that.setData({
            ishaspep: false
          })
        }
      }
    }).catch(err => {
      Toast.clear()
      
      that.setData({
        ishaspep: false,
        isshow: true
      })
    })


  },
  /**
   * 跳到添加账套页
   */
  toaddcurm() {
    wx.navigateTo({
      url: '../bindCard/addcard/index',
    })
  }
})